<?php
/**
 *  @copyright 2010
 *  @package _version@
 *  @author oriotiori
 */

class Admin_Model_News extends Zend_Db_Table_Abstract
{
	protected $_name = 'admin_news';

/**
 * Método para convertir  a seo
 * @param $s
 */
    public function convertir_seo($s) {
        $s = strtolower($s);
        $s = @ereg_replace("[áàâãäª@]","a",$s);
        $s = @ereg_replace("[éèêë]","e",$s);
        $s = @ereg_replace("[íìîï]","i",$s);
        $s = @ereg_replace("[óòôõºö]","o",$s);
        $s = @ereg_replace("[úùûü]","u",$s);
        $s = @ereg_replace("[ç]","c",$s);
        $s = @ereg_replace("[ñ]","n",$s);
        $s = @preg_replace( "/[^a-zA-Z0-9\-]/", "-", $s );
        $s = @preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $s);

        return trim($s, '-');
    }

    
/**
 * Método para agregar una nueva Noticia
 * @param  array $new
 * @return  mixed $res
 */
	function addNews($new)
	{
	    try{
	    	$data = array(
                    'Title'             => $new['Title'],
                    'Seo'               => $new['Seo'],
                    'Language'          => $new['Language'],
                    'Creation_Date'     => new Zend_Db_Expr('NOW()'),
                    'Short_Description' => $new['ShortDescription'],
                    'Long_Description'  => $new['LongDescription'],
                    'Modified_By'       => $new['ModifiedBy'],
                    'Category_Id'       => $new['CategoryId'],
                    'Company_Id'        => $new['CompanyId']
                );

                $res=$this->getAdapter()->insert($this->_name,$data);
                $new['NewsId']= (int)$this->getAdapter()->lastInsertId();
                
                return $res;
	    }
            catch(Exception $e)
	    {
            	echo 'Caught exception: ',  $e->getMessage(), "\n";
	    }
	}

/**
 * método para actualizar una noticia
 * @param  array $new : array de datos a actualizar
 * @return mixed $res
 */
	function updateNews($new)
	{
	    try
            {
	    	$data = array(
                    'Title'             => $new['Title'],
                    'Seo'               => $new['Seo'],
                    'Short_Description' => $new['ShortDescription'],
                    'Long_Description'  => $new['LongDescription'],
                    'Modified_By'       => $new['ModifiedBy']
                );
                
                $where[]="Id=".$new['Id'];

                $res = $this->getAdapter()->update($this->_name, $data, $where);
                return $res;                
	    }
            catch(Exception $e)
	    {
	    	echo 'Caught exception: ',  $e->getMessage(), "\n";
	    }
	}

/**
 *Método para leer los datos de una noticia específica
 * @param  array $new
 * @return array $control
 */
        function getNews($new)
	{
            $TONews = new Admin_Model_TO_News();
            $x_id = $new['Id'];

	    if($x_id >0){

                $select= $this
                            ->getAdapter()
                            ->select()
                            ->from(array('an' => $this->_name))
                            ->where('an.Category_Id=?', $new['CategoryId'])
                            ->where('an.Id=?', $x_id)
                            ->where('an.Status=?', '0')
                            ->limit(1)
                ;

                $data = $this->getAdapter()->fetchAll($select);
                $rowCount = count($data);

                try{
                    if ($rowCount > 0)
                    {
                        $TONews->setId($data[0]['Id']);
                        $TONews->setShortDescription($data[0]['Short_Description']);
                        $TONews->setTitle($data[0]['Title']);
                        $TONews->setCategoryId($data[0]['Category_Id']);
                        $TONews->setLongDescription($data[0]['Long_Description']);
                        $TONews->setSeo($data[0]['Seo']);

                        $control = $TONews->getAllVars();
                        return $control;
                    }
                    else{
                        throw new Exception('No found results.');
                    }
                }
                catch(Exception $e)
                {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                }
            }
	}

/**
 *Método para leer los idiomas
 * @return array $control
 */
        function getLanguages()
	{
            $TO = new Admin_Model_TO_News();

                $select= $this
                            ->getAdapter()
                            ->select()
                            ->from('admin_languages')
                            ->where('Status=?', '0');
                $data = $this->getAdapter()->fetchAll($select);

                try{
                    foreach ($data as $dt){
                        
                        $TO->setId($dt['Id']);
                        $TO->setIso($dt['Iso']);
                        $TO->setLanguage($dt['Language']);
                        $TO->Add();    
                    }
                    $control = $TO->getAllData();
                    return $control;
                }
                catch(Exception $e)
                {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                }
	}

/**
 *Método para leer los datos de una noticia específica por Seo
 * @param  array $new
 * @return array $control
 */
        function getNewsBySeo($new)
	{
            $TONews = new Admin_Model_TO_News();
            $seo = $new['News'];

            
	    if(!empty($seo)){

                $select= $this
                            ->getAdapter()
                            ->select()
                            ->from(array('an' => $this->_name), '*')
                            ->joinInner(
                                    array('l' => 'admin_languages'),
                                    'an.language=l.Id',
                                    array('Iso'))
                            ->where('l.Iso=?', $new['Language'])
                            ->where('an.Seo=?', $seo)
                            ->where('an.Status=?', '0')
                            ->limit(1);

                $data = $this->getAdapter()->fetchAll($select);
                $rowCount = count($data);

                try{
                    if ($rowCount > 0)
                    {
                        $TONews->setId($data[0]['Id']);
                        $TONews->setShortDescription($data[0]['Short_Description']);
                        $TONews->setTitle($data[0]['Title']);
                        $TONews->setCategoryId($data[0]['Category_Id']);
                        $TONews->setLongDescription($data[0]['Long_Description']);
                        $TONews->setToururl($data[0]['Seo']);

                        $control = $TONews->getAllVars();

                        return $control;
                    }
                    else
                    {
                        throw new Exception('No found results.');
                    }
                }
                catch(Exception $e)
                {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
                }
            }
	}

/**
 *Método para leer todas las noticias
 * @param array $new 
 * @return array $control  : array de datos
 */
	function getAllNews($new)
	{

            $TONews = new Admin_Model_TO_News();

            try{
                $select= $this
                            ->getAdapter()
                            ->select()
                            ->from(array('an' => $this->_name), '*')
                            ->joinInner(
                                    array('l' => 'admin_languages'),
                                    'an.language=l.Id',
                                    array('Iso'))
                            ->where('an.Status=?', '0')
                            ->where('l.Iso=?', $new['Language'])
                            ->where('an.Category_Id=?', $new['CategoryId'])
                            ->where('an.Company_Id=?', $new['CompanyId'])
                            ->order('an.Id ASC');

                $data = $this->getAdapter()->fetchAll($select);

                foreach ($data as $dt){

                    $TONews->setId($dt['Id']);
                    $TONews->setTitle($dt['Title']);
                    $TONews->setShortDescription($dt['Short_Description']);
                    $TONews->setLongDescription($dt['Long_Description']);
                    $TONews->setToururl($dt['Seo']);

                    $TONews->add();
                }

                $datao = $TONews->getAllData();

                return $datao;

            }catch(Exception $e)
            {
                    echo 'Caught exception: ',  $e->getMessage(), "\n";
            }
	}	
/**
 * Método para eliminar una categoria
 * @param  object $new
 * @return mixed $res
 */
	function deleteNews($new)
	{
            $data = array(
  		'Status'	=>  '1',
                'Modified_By' 	=>  $new->getModifiedBy()
            );
            $where[]="Id=".$new->getId();
            $where[]="Category_Id=".$new->getCategoryId();
            
	    try{
               $res = $this->getAdapter()->update($this->_name, $data, $where);
               return $res;
	    }
            catch(Exception $e)
	    {
	    	echo 'Caught exception: ',  $e->getMessage(), "\n";
	    }
	}
}
